package com.huawei.M01.iterator;

import java.util.Stack;

public class DecodeString {
    public String decodeString(String s) {
        int tmp = 0;
        // 用于存放需要重复的字符串
        Stack<String> stringStack = new Stack<>();
        Stack<Integer> integerStack = new Stack<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (Character bytes : s.toCharArray()) {
            if (bytes - '0' >= 0 && bytes - '0' <= 9) {
                tmp = tmp * 10 + (bytes - '0');
            }else if (bytes == '[') {
                // 遇到'['就讲tem放进堆栈中，然后重置tmp
                integerStack.push(tmp);
                stringStack.push(stringBuilder.toString());
                stringBuilder = new StringBuilder();
                tmp = 0;
            }else if (bytes == ']'){
                StringBuilder sb = new StringBuilder();
                Integer pop = integerStack.pop();
                for (int i = 0; i < pop; i++) {
                    sb.append(stringBuilder);
                }
                stringBuilder = new StringBuilder(stringStack.pop() + sb);
            }else {
                stringBuilder.append(bytes);
            }
        }
        return stringBuilder.toString();
    }

    private int getNumber(String s, int startIndex, int i) {

        return 0;
    }
}
